package com.e.takeaway;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.e.takeaway.po.User;
import com.e.takeaway.service.RequestService;
import com.e.takeaway.util.MyRetrofit;

import java.io.IOException;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class UpdatePawActivity extends AppCompatActivity {
    private static final String FILE_NAME = "user";
    private EditText ed_old,ed_new,ed_re;
    private Button bt_1;
    // private static int a;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_update_paw);
        ed_old = (EditText) findViewById(R.id.old_pwd);
        ed_new = (EditText) findViewById(R.id.new_pwd);
        ed_re = (EditText) findViewById(R.id.req_pwd);
        SharedPreferences sp =getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        String phone = sp.getString("phone","");
        String password = sp.getString("pwd","");
        bt_1 = (Button) findViewById(R.id.bt_queren);
        this.setTitle("密码修改");

        bt_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String ned = ed_old.getText().toString().trim();
                String pwd = ed_new.getText().toString().trim();
                String req = ed_re.getText().toString().trim();
                if (!"".equals(ned) && !"".equals(pwd) && !"".equals(req)){
                    if (ned.equals(password)){
                        if (pwd.equals(req)){
                            request(phone,req);
                        }else
                            Toast.makeText(UpdatePawActivity.this,"2次密码不匹配",Toast.LENGTH_SHORT).show();
                    }else{
                        Toast.makeText(UpdatePawActivity.this,"原密码错误",Toast.LENGTH_SHORT).show();
                    }
                }else{
                    Toast.makeText(UpdatePawActivity.this,"输入框不能为空",Toast.LENGTH_SHORT).show();
                }
            }
        });

    }


    private void request(String phone,String pwd) {
        Retrofit retrofit = MyRetrofit.getRetrofit();
        RequestService request = retrofit.create(RequestService.class);
        Call<Integer> call = request.updatePwd(phone,pwd);
        call.enqueue(new Callback<Integer>() {
            @Override
            public void onResponse(Call<Integer> call, Response<Integer> response) {
                int res = response.body();
                if (1 == res){
                    SharedPreferences.Editor edit = getApplicationContext().getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE).edit();
                    edit.clear();
                    edit.commit();
                    Toast.makeText(UpdatePawActivity.this,"修改成功",Toast.LENGTH_SHORT).show();
                    startActivity(new Intent(UpdatePawActivity.this, LoginActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK));
                    finish();
                }else {
                    Toast.makeText(UpdatePawActivity.this,"修改失败",Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<Integer> call, Throwable t) {

            }
        });
    }
}